package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //1、封装参数数据
        //把map转化成json字符串
        String hospJsonStr = JSONObject.toJSONString(paramMap);
        //把json字符串转化成对象
                Hospital hospital = JSONObject.parseObject(hospJsonStr, Hospital.class);

        //2、判断医院信息是否存在
        Hospital targetHospital =
                hospitalRepository.getHospitalByHoscode(hospital.getHoscode());

        if(targetHospital!=null){
            //3、mongo里有此医院信息，更新
            hospital.setStatus(targetHospital.getStatus());
            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospital.setId(targetHospital.getId());
            hospitalRepository.save(hospital);

        }else{
            //3、mongo里没有此医院信息，新增
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);

        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    //带分页、条件查询医院信息列表
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        // 1创建排序对象
        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");
        //2设置分页对象，0是第一页
        Pageable pageable = PageRequest.of(page-1,limit,sort);
        //3设置查询条件
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        //4,设置查询规则
        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true);
        //5创建模板实例
        Example<Hospital> example = Example.of(hospital,matcher);
        Page<Hospital> hospitalPage = hospitalRepository.findAll(example, pageable);

        //TODO 跨模块调用查询字典服务，翻译编码信息
        //遍历封装医院等级数据、地址相关信息
        hospitalPage.getContent().stream().forEach(item->{
            this.packHospital(item);
        });

        return hospitalPage;
    }

    private Hospital packHospital(Hospital hospital) {
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(),hospital.getHostype());
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString + hospital.getAddress());
        return hospital;

    }


}
